package com.ktwlsoft.fundmanage.domain.repository;

import com.ktwlsoft.fundmanage.domain.entity.BaseFundRecord;
import com.ktwlsoft.fundmanage.domain.entity.FundOutRecord;
import com.ktwlsoft.fundmanage.domain.entity.BaseFundRecord.State;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public interface FundOutRecordRepository extends JpaRepository<FundOutRecord, Long> {

        FundOutRecord findByFCode(long fcode);

        @Modifying
        @Query(value = "update FundOutRecord f set f.expenditureCheck=:checked, f.expenditureTime=:expenditureTime where "
                        + "f.fCode=:fCode")
        int checkAccount(long fCode, boolean checked, Date expenditureTime);

        @Modifying
        @Query(value = "update FundOutRecord f set f.isRevoke = true where f.fCode=:fCode")
        int revoke(long fCode);

        FundOutRecord findByDnCode(long otherTransferId);

        FundOutRecord findByDnCodeAndStateNot(long transId, BaseFundRecord.State state);

        @Modifying
        @Query(value = "update FundOutRecord  f set f.expenditureTime=:now, f.expenditureChecker=:operator, "
                        + "f.expenditureCheckerId=:operatorId, f.isRevoke=:isRevoke, f.state=:state, f.expenditureCheck=:check, f.expenditureTime=:expenditureTime where f.fCode=:fCode")
        int updateFundOutExp(long fCode, Date now, String operator, String operatorId, boolean isRevoke,
                        BaseFundRecord.State state, boolean check, Date expenditureTime);

        boolean existsByFCode(long fCode);

        @Modifying
        @Query(value = "update FundOutRecord f set f.state = :state where f.fCode=:fCode")
        int updateState(BaseFundRecord.State state, long fCode);

        @Query("select sum(f.amount) from FundOutRecord f where f.tsCode = :tsCode and f.state='NORMAL'")
        BigDecimal queryLoan(Long tsCode);

        List<FundOutRecord> findByNetSignCodeAndState(String netWorkSign, State state);
}
